//using Microsoft.VisualBasic;
using System;
using NHibernate.Cfg;
using NHibernate;
using System.Diagnostics;
using System.Collections;
public partial class Formtest2
{
	NHibernate.ISession session;
	//Dim addnew As Boolean
	//Dim thenew As NCS.muser

	NCS.muser current;

	string currentname;
	private void MuserBindingSource_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
	{
		log("add new");
		//addnew = True
		//thenew = New NCS.muser
		//e.NewObject = thenew
	}

	private void MuserBindingSource_BindingComplete(object sender, System.Windows.Forms.BindingCompleteEventArgs e)
	{
		log("bind complete");
	}

	private void MuserBindingSource_CurrentChanged(object sender, System.EventArgs e)
	{
		log("current change");
		if ((current != null)) {
			log("current:" + currentname + "," + current.mname);
			if (current.mname != currentname) {
				log("name changed");
				if (string.IsNullOrEmpty(currentname)) {
					log("new user:" + current.mname);
					session.Save(current);
					session.Flush();
				} else {
					log("can not change name");
					current.mname = currentname;
				}
			} else {
				if (session.IsDirty()) {
					session.Flush();
				}
			}
		}
		current = this.MuserBindingSource.Current as NCS.muser;
		currentname = current.mname;
		log("current:" + current.mname);

	}
	public void log(string s)
	{
		Debug.Print(Convert.ToString(System.DateTime.Now) + ":" + s);
	}
	private void MuserBindingSource_CurrentItemChanged(object sender, System.EventArgs e)
	{
		log("current item change");

	}

	private void MuserBindingSource_DataError(object sender, System.Windows.Forms.BindingManagerDataErrorEventArgs e)
	{
		log("data error");
	}

	private void MuserBindingSource_DataMemberChanged(object sender, System.EventArgs e)
	{
		log("data member change");
	}

	private void MuserBindingSource_DataSourceChanged(object sender, System.EventArgs e)
	{
		log("data source change");
	}

	private void MuserBindingSource_Disposed(object sender, System.EventArgs e)
	{
		log("dispose");
	}

	private void MuserBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
	{
		//log("list change")
		//log(CStr(e.ListChangedType))
		//Select Case e.ListChangedType
		//    Case System.ComponentModel.ListChangedType.ItemDeleted
		//        log("list change:delete")
		//        log(CStr(e.NewIndex))
		//        log(CStr(e.OldIndex))
		//    Case System.ComponentModel.ListChangedType.ItemAdded
		//        log("list change:added")
		//    Case ComponentModel.ListChangedType.ItemChanged
		//        log("list change:item changed")
		//        If current.mname <> currentname Then
		//            log("name changed")
		//            If currentname = "" Then
		//                log("new user:" + current.mname)
		//                session.Save(current)
		//                session.Flush()
		//            Else
		//                log("can not change name")
		//                current.mname = currentname
		//            End If
		//        Else
		//            log("flush")
		//            session.Flush()
		//        End If
		//    Case ComponentModel.ListChangedType.ItemMoved
		//        log("list change:item moved")
		//    Case ComponentModel.ListChangedType.PropertyDescriptorAdded
		//        log("property added")
		//    Case ComponentModel.ListChangedType.PropertyDescriptorChanged
		//        log("prop change")
		//    Case ComponentModel.ListChangedType.PropertyDescriptorDeleted
		//        log("prop del")
		//    Case ComponentModel.ListChangedType.Reset
		//        log("reset")
		//End Select
	}

	private void MuserBindingSource_PositionChanged(object sender, System.EventArgs e)
	{
		log("position chaned");
	}


	public Formtest2()
	{
		// This call is required by the Windows Form Designer.
		InitializeComponent();

		// Add any initialization after the InitializeComponent() call.
		Configuration c = NCS.Class1.configuration;
		ISessionFactory factory = c.BuildSessionFactory();
		//Dim firstId As String
		session = factory.OpenSession();
		IList l = null;
		log("add.....");
		l = session.CreateCriteria(typeof(NCS.muser)).List();
		foreach (NCS.muser i in l) {
			this.MuserBindingSource.Add(i);
		}
		log("add finish");
        //this.MuserDataGridView.Columns(0).HeaderText = "编号";
	}

	private void BindingNavigatorDeleteItem_Click(object sender, System.EventArgs e)
	{
		if ((this.MuserBindingSource.Current != null)) {
			session.Delete(this.MuserBindingSource.Current);
			log("delete");
			session.Flush();
		}
	}

    private void BindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
    {

    }
}
